<template>
  <div class="box">
    <div class="top_menu">
      <com-title tit_txt="样本库标引项"></com-title>
      <div class="operate">
        <el-button
          class="addBtn"
          type="primary "
          @click="addRow"
          size="small"
          style="margin-bottom: 10px"
          ><i class="el-icon-plus" /> 添加标引页</el-button
        >
      </div>
    </div>
    <el-table
      width
      :data="indexList"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="标引项名称" align="center">
        <template slot-scope="scope">
          <span v-if="!scope.row.disabled">{{ scope.row.biaoyinxmc }}</span>
          <el-input
            v-if="scope.row.disabled"
            v-model="scope.row.biaoyinxmc"
            placeholder="标引项名称"
          />
        </template>
      </el-table-column>
      <el-table-column label="标引项类型" align="center">
        <template slot-scope="scope">
          <span v-if="!scope.row.disabled">{{ scope.row.biaoyinxlx }}</span>
          <el-select
            v-model="scope.row.biaoyinxlx"
            placeholder="请选择类型"
            v-if="scope.row.disabled"
          >
            <el-option
              v-for="item in indexTypeList"
              :key="item.name"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="下拉框参数" align="center">
        <template slot-scope="scope" v-if="scope.row.biaoyinxlx == 'select'">
          <span v-if="!scope.row.disabled">{{ scope.row.biaoyinz }}</span>
          <el-input
            v-model="scope.row.biaoyinz"
            v-if="scope.row.disabled"
            placeholder="下拉框参数"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.edit"
            size="mini"
            icon="el-icon-document"
            @click="confirmEdit(scope.row)"
            >保存
          </el-button>
          <el-button v-else size="mini" @click="startEdit(scope.row)"
            ><i class="el-icon-edit-outline" /> 编辑
          </el-button>
          <el-button
            v-if="scope.row.edit"
            class="cancel-btn"
            size="mini"
            icon="el-icon-close"
            @click="cancelEdit(scope.$index, scope.row)"
            >取消
          </el-button>
          <el-button
            v-if="!scope.row.edit"
            type="danger"
            size="mini"
            @click="handleDelete(scope.row, scope.$index)"
            ><i class="el-icon-delete" /> 删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { indexAdd, indexEdit, indexDel } from "@/api/sampleBank";
import comTitle from "./comTitle.vue";
export default {
  components: { comTitle },
  name: "",
  props: ["indexList", "yangbenkid"],
  data() {
    return {
      indexTypeList: [
        {
          name: "文本",
          value: "text",
        },
        {
          name: "时间",
          value: "date",
        },
        {
          name: "下拉框",
          value: "select",
        },
      ],
    };
  },
  watch: {
    indexList: {
      handler(news, old) {
        console.log(news, old);
      },
      immediate: true,
    },
  },
  mounted() {},
  methods: {
    //删除
    handleDelete(row, index) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          row.yangbenkid = this.yangbenkid
          indexDel(row).then((res) => {
            if (res.code === 200) {
              this.$message({
                message: "删除成功!",
                type: "success",
              });
              this.$emit("getList", this.yangbenkid);
            }
          });
        })
        .catch(() => {});
    },
    // 编辑
    startEdit(row) {
      console.log(row)
      row.edit = !row.edit;
      row.disabled = true;
    },

    // 保存
    confirmEdit(row) {
      if (!row.biaoyinxlx || !row.biaoyinxmc) {
        return this.$message.warning("请填写完整");
      }
      row.edit = false;
      row.disabled = false;
      row.biaoyinxxz = row.biaoyinz
      delete row.biaoyinz
      const params = { ...row, yangbenkid: this.yangbenkid };
      if (row.status == "add") {
        indexAdd(params).then((res) => {
          if (res.code === 200) {
            this.$message({
              message: "添加成功!",
              type: "success",
            });
            this.$emit("getList", this.yangbenkid);
          }
        });
      } else {
        indexEdit(params).then((res) => {
          if (res.code === 200) {
            this.$message({
              message: "编辑成功!",
              type: "success",
            });
            this.$emit("getList", this.yangbenkid);
          }
        });
      }
    },

    // 取消
    cancelEdit(index, row) {
      if (row.status == "add") {
        this.indexList.splice(index, 1);
      } else {
        row.edit = false;
        row.disabled = false;
      }
    },
    // 增加行
    addRow() {
      let newLine = {
        biaoyinxmc: "",
        biaoyinxlx: "",
        biaoyinxxz: "",
        status: "add", //新增/编辑
        edit: true, // 新增时处于可编辑状态，所以按钮是保存和取消
        disabled: true, // 打开编辑状态
      };
      this.indexList.unshift(newLine); // 移到第一行
    },
  },
};
</script>

<style  lang="less" scoped>
.box {
  margin-bottom: 20px;
  .top_menu {
    display: flex;
    justify-content: space-between;
    margin-bottom: 15px;
  }
}
</style>